package cn.chaos.bio01;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class CompressClient {
    
    public static final int BUFSIZE = 256; 
    
    public static void main(String[] args) throws IOException  {
        
        int port = 11111;
        String server = "localhost";
        
        String filename = "D:\\Books\\念伟大领袖毛主席.txt";
        
        FileInputStream fileIn = new FileInputStream(filename);
        
        FileOutputStream fileOut = new FileOutputStream(filename + ".gz");
        
        Socket socket = new Socket(server, port);
        
        sendBytes(socket, fileIn);
        
        InputStream sockIn = socket.getInputStream();
        int bytesRead;
        
        byte[] buffer = new byte[BUFSIZE];
        
        while ((bytesRead = sockIn.read(buffer)) != -1) {
            fileOut.write(buffer, 0, bytesRead);
            System.out.print("R");
        }
        System.out.println();
        
        socket.close();
        
        fileIn.close();
        fileOut.close();
        
        
    }

    private static void sendBytes(Socket socket, InputStream fileIn) throws IOException {
        
        OutputStream socketOut = socket.getOutputStream();
        int bytesRead;
        
        byte[] buffer = new byte[BUFSIZE];
        
        while ((bytesRead = fileIn.read(buffer)) != -1) {
            socketOut.write(buffer, 0, bytesRead);
            System.out.print("W");
        }
        System.out.println();
        socket.shutdownOutput();
    }
}
